import java.io.*;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @ClassName Main
 * @Description: 嵌入式封装入口
 * @Author wangyongtao
 * @Date 2023/2/18 9:27
 * @Version 1.0
 **/
public class Main {


    public static void main(String[] args) {
        System.out.println("嵌入式部署开始...");

        InputStream in = null;
        InputStream error = null;
        try {
            //1、解压
            String basePath = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();
            basePath = java.net.URLDecoder.decode(basePath, "UTF-8");
            int lastIndex = basePath.lastIndexOf(File.separator) + 1;
            String jarName = basePath.substring(lastIndex, basePath.length());
            System.out.println("当前jar：" + jarName);
            basePath = basePath.substring(0, lastIndex);
            System.out.println("当前路径为：" + basePath);

            String unzipPath = basePath + "temp" + System.currentTimeMillis() + "/";

            String[] cmd = new String[]{"/bin/sh", "-c", "unzip " + basePath + jarName + " -d " + unzipPath, "chmod 777 -R " + unzipPath};
            System.out.println("解压jar包执行命令：" + Arrays.asList(cmd).stream().collect(Collectors.joining(",")));
            Process pro = Runtime.getRuntime().exec(cmd);
            in = pro.getInputStream();
            error = pro.getErrorStream();
            BufferedReader read = new BufferedReader(new InputStreamReader(in));
            String result = null;
            while ((result = read.readLine()) != null) {
                System.out.println("解压信息:" + result);
            }
            BufferedReader read2 = new BufferedReader(new InputStreamReader(error));
            String result2 = null;
            while ((result2 = read2.readLine()) != null) {
                System.out.println("解压错误信息:" + result2);
            }

            pro.waitFor();
            System.out.println("解压结果(0表示成功，其余为失败)：" + pro.exitValue());

            in.close();
            error.close();
            read.close();
            read2.close();

            //2、install
            String execInstallBashCmd = "bash " + unzipPath + "install.sh " + unzipPath;
            System.out.println("install脚本执行命令：" + execInstallBashCmd);
            pro = Runtime.getRuntime().exec(execInstallBashCmd);
            in = pro.getInputStream();
            error = pro.getErrorStream();
            read = new BufferedReader(new InputStreamReader(in));
            while ((result = read.readLine()) != null) {
                System.out.println("install执行信息:" + result);
            }
            read2 = new BufferedReader(new InputStreamReader(error));
            while ((result2 = read2.readLine()) != null) {
                System.out.println("install错误信息:" + result2);
            }

            pro.waitFor();
            System.out.println("install结果(0表示成功，其余为失败)：" + pro.exitValue());

            in.close();
            error.close();
            read.close();
            read2.close();

            //3、operate
            String execOperateBashCmd = "bash " + unzipPath + "operate.sh " + unzipPath;
            System.out.println("operate脚本执行命令：" + execOperateBashCmd);
            pro = Runtime.getRuntime().exec(execOperateBashCmd);
            in = pro.getInputStream();
            error = pro.getErrorStream();
            read = new BufferedReader(new InputStreamReader(in));
            while ((result = read.readLine()) != null) {
                System.out.println("operate执行信息:" + result);
            }
            read2 = new BufferedReader(new InputStreamReader(error));
            while ((result2 = read2.readLine()) != null) {
                System.out.println("operate错误信息:" + result2);
            }

            pro.waitFor();
            System.out.println("operate结果(0表示成功，其余为失败)：" + pro.exitValue());

            in.close();
            error.close();
            read.close();
            read2.close();

            System.out.println("部署结束！");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(getErrorMsg(e));
        }


    }

    public static String getErrorMsg(Throwable thr) {
        StringWriter stringWriter = new StringWriter();
        try (PrintWriter printWriter = new PrintWriter(stringWriter);) {
            thr.printStackTrace(printWriter);
            return stringWriter.toString();
        }
    }


}